package com.hmall.trade.listener;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author LiYang
 * @desc
 * @date 2024/10/6 17:28
 */
@Component
@RequiredArgsConstructor
public class PayStatusListener {
    private final IOrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "trade.pay.success.queue", durable = "true"),
            exchange = @Exchange("pay.direct"),  //有点不太懂为什么是direct类型的交换机可以使用 *.*类型的key
            key = "pay.success"
    ))
    public void listenPaySuccess(Long orderId){
        //业务幂等 1、设置消息唯一ID 2、基于业务做逻辑判断
        // 1.查询订单
        Order order = orderService.getById(orderId);
        // 2.判断订单状态是否是未支付
        if (order == null || order.getStatus() != 1){
            // 订单不存在或状态不是未支付  不做处理
            return;
        }
        // 3.标记订单为已支付
        orderService.markOrderPaySuccess(orderId);
    }
}
